//
//  GanLuXinQingSaver.m
//  GanLuApp
//
//  Created by luoy on 2024/4/3.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "GanLuXinQingSaver.h"

@implementation GanLuXinQingSaver

+ (void)saveDate:(NSDate *)date time:(NSDate *)time img:(UIImage *)img tagType:(NSInteger)tagType content:(NSString *)content {
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy/MM/dd";
    NSString *dateStr = [df stringFromDate:date];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{
        @"date": date,
        @"tagType": @(tagType)
    }];
    
    if(content){
        dic[@"content"] = content;
    }
    if (time) {
        dic[@"time"] = time;
    }
    if (img) {
        NSData *imageData = UIImageJPEGRepresentation(img, 1);
        dic[@"img"] = imageData;
    }
    NSDictionary *temp = [[NSUserDefaults standardUserDefaults] valueForKey:@"userxinqing"];
    NSMutableDictionary *allDic = [NSMutableDictionary dictionaryWithDictionary:temp];
    if (allDic){
        [allDic setValue:dic forKey:dateStr];
        [[NSUserDefaults standardUserDefaults] setValue:allDic forKey:@"userxinqing"];
        return;
    }
    [[NSUserDefaults standardUserDefaults] setValue:@{dateStr: dic} forKey:@"userxinqing"];
}

+ (NSDictionary *)date:(NSDate *)date {
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy/MM/dd";
    NSString *dateStr = [df stringFromDate:date];
    
    NSDictionary *allDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"userxinqing"];
    if ([allDic valueForKey:dateStr]){
        return [allDic valueForKey:dateStr];
    }
    return nil;
}

+ (NSDictionary *)dateString:(NSString *)dateStr {
    
    
    NSDictionary *allDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"userxinqing"];
    if ([allDic valueForKey:dateStr]){
        return [allDic valueForKey:dateStr];
    }
    return nil;
}

+ (NSArray *)monthString:(NSString *)dateStr {
    
    
    NSDictionary *allDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"userxinqing"];
    NSArray *allkeys = allDic.allKeys;
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy/MM/dd";
    NSDate *currentDate = [df dateFromString:dateStr];
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in allkeys) {
        NSDate *date = [df dateFromString:str];
        if ([self year:date] == [self year:currentDate] && [self month:date] == [self month:currentDate]) {
            [arr addObject:allDic[str]];
        }
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES selector:@selector(compare:)];
    
    NSArray *sortedDates = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
    return arr;
}

+ (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components month];
}

//年份
+ (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components year];
}



@end
